package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.file.server.FileStorageService;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.wemwdia.dto.WmMaterialDto;
import com.zhentao.model.wemwdia.pojo.WmMaterial;
import com.zhentao.service.WmMaterialService;
import com.zhentao.utils.common.WmUserThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private FileStorageService fileStorageService;
    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
//        参数校验
        if(multipartFile == null || multipartFile.getSize() <=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String url = fileStorageService.uploadHtmlImage(multipartFile.getOriginalFilename()
                , multipartFile.getInputStream());
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUserId(WmUserThreadLocalUtil.getUser().getId());
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(url);
        wmMaterialMapper.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
//        校验参数
        dto.checkParam();
//        分页
        //根据用户Id查询
        QueryWrapper<WmMaterial> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmMaterial::getUserId, WmUserThreadLocalUtil.getUser().getId());
        //排序    应该根据id来排序    能不能根据创建时间来排序  ？
        //  修改时间  1  2   3
        queryWrapper.orderByDesc("id");
        //是否收藏
        queryWrapper.lambda().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());
        Page<WmMaterial> page = wmMaterialMapper.selectPage(new Page<WmMaterial>(dto.getPage(),dto.getSize()), queryWrapper);
        PageResponseResult pageResponseResult=new PageResponseResult();
        pageResponseResult.setSize(Integer.valueOf(page.getSize()+""));
        pageResponseResult.setTotal(Integer.valueOf(page.getTotal()+""));
        pageResponseResult.setCurrentPage(Integer.valueOf(page.getCurrent()+""));
        pageResponseResult.setCode(200);
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }
}
